package com.jumi.microservice.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jumi.microservice.common.constant.RequestConstant;
import com.jumi.microservice.constant.ExceptionEnum;
import com.jumi.microservice.constant.StatusEnum;
import com.jumi.microservice.domain.ArticleClass;
import com.jumi.microservice.domain.dto.ArticleClassDTO;
import com.jumi.microservice.mapper.ArticleClassMapper;
import com.jumi.microservice.service.ArticleClassService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class ArticleClassServiceImpl extends ServiceImpl<ArticleClassMapper, ArticleClass> implements ArticleClassService {

    @Resource
    private ArticleClassMapper articleClassMapper;

    @Override
    public void insert(ArticleClassDTO dto, HttpServletRequest request) {
        ExceptionEnum.FAIL_NULL.doThrowIf(dto==null);
        //分类名称不能重复
        int acNameCount =   articleClassMapper.selectCount(
                Wrappers.<ArticleClass>lambdaQuery()
                .eq(ArticleClass::getAcName,dto.getAcName())
                .eq(ArticleClass::getIsDel,StatusEnum.STATUS_NO.getCode())
        );
        ExceptionEnum.FAIL_ARTICLECLASS_TITLE.doThrowIf(acNameCount > 0);
        //判断分类是否是一级分类
        if(dto.getAcParentId()>0){
            //获取分级分类id信息
            ArticleClass articleClass = articleClassMapper.selectById(dto.getAcParentId());
            if(articleClass.getAcParentId() > 0){
                dto.setAcParentId(articleClass.getAcParentId());
            }else{
                dto.setAcParentId(articleClass.getAcid());
            }
        }
        ArticleClass r = dto2Po(dto);
        r.setCreater(request.getHeader(RequestConstant.ADMIN_NAME));
        r.setModifier(request.getHeader(RequestConstant.ADMIN_NAME));
        baseMapper.insert(r);
    }

    @Override
    public void update(ArticleClassDTO dto,HttpServletRequest request) {
        ExceptionEnum.FAIL_NULL.doThrowIf(dto==null);
        //分类名称不能重复
        int acNameCount =   articleClassMapper.selectCount(
                Wrappers.<ArticleClass>lambdaQuery()
                        .ne(ArticleClass::getAcid, dto.getAcid())
                        .eq(ArticleClass::getAcName,dto.getAcName())
                        .eq(ArticleClass::getIsDel,StatusEnum.STATUS_NO.getCode())
        );
        ExceptionEnum.FAIL_ARTICLECLASS_TITLE.doThrowIf(acNameCount > 0);
        //判断分类是否是一级分类
        if(dto.getAcParentId()>0){
            //获取分级分类id信息
            ArticleClass articleClass = articleClassMapper.selectById(dto.getAcParentId());
            if(articleClass.getAcParentId() > 0){
                dto.setAcParentId(articleClass.getAcParentId());
            }else{
                dto.setAcParentId(articleClass.getAcid());
            }
        }
        ArticleClass r = dto2Po(dto);
        r.setModifier(request.getHeader(RequestConstant.ADMIN_NAME));
        baseMapper.updateById(r);
    }

    @Override
    public void delete(String id, HttpServletRequest request) {
        String modifier = request.getHeader(RequestConstant.ADMIN_NAME);
        //用逗号将字符串分开，得到字符串数组
        String[] strs = id.split(",");
        for (String ids : strs) {
            //查询信息
            ArticleClass articleClass1 = articleClassMapper.selectById(ids);
            ExceptionEnum.FAIL_ARTICLECLASS_DEL2.doThrowIf(articleClass1.getStatus() == 1);

            ArticleClass articleClass2 = articleClassMapper.selectOne(Wrappers.<ArticleClass>lambdaQuery()
                    .eq(ArticleClass::getAcParentId,ids)
                    .eq(ArticleClass::getIsDel,StatusEnum.STATUS_NO.getCode()));
            ExceptionEnum.FAIL_ARTICLECLASS_DEL2.doThrowIf(articleClass2 != null);
        }
        //将字符串数组转换成集合
        List idArr= Arrays.asList(strs);

        articleClassMapper.delete(idArr,modifier);
    }


    @Override
    public ArticleClassDTO selectById(Long id) {
        ArticleClass articleClass = baseMapper.selectById(id);
        ArticleClassDTO dto = po2Dto(articleClass);
        return dto;
    }

    @Override
    public void status(Long id, Integer status, HttpServletRequest request) {
        ArticleClass articleClass = baseMapper.selectById(id);
        articleClass.setStatus(status);
        articleClass.setModifier(request.getHeader(RequestConstant.ADMIN_NAME));
        baseMapper.updateById(articleClass);

    }


    /**
     * 对象转换 dto->do
     * @param dto
     * @return
     */
    private ArticleClass dto2Po(ArticleClassDTO dto){
        ArticleClass r = null;
        try {
            r = dto.clone(ArticleClass.class);
        }catch (Exception e){
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return r;
    }

    /**
     * 对象转换 po->dto
     * @param po
     * @return
     */
    private ArticleClassDTO po2Dto(ArticleClass po){
        ArticleClassDTO r = null;
        try {
            r = po.clone(ArticleClassDTO.class);
        }catch (Exception e){
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return r;
    }
}
